﻿/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：chenkai
 * 日  期：2022-02-24
 * 描  述：属性设置
 */
var refreshGirdData;
var PropertyGID = "";
var ObjectTypeID = request('ObjectTypeID');
var selectData = [];
var ProjectId = request('ProjectId');
var EngineDataID = request('EngineDataID');
var CaseID = request('CaseID');
var showHistory;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.bind();
            page.initGird();
        },
        bind: function () {
            // 初始化左侧树形数据
            $('#dataTree').lrtree({
                url: top.$.rootUrl + '/ZZDT_EC/ec_property/GetTree?ProjectId=' + ProjectId,
                nodeClick: function (item) {
                    $("#titleinfo").html(item.text);
                    PropertyGID = item.value;
                    page.search();
                }
            });

            //获取当前选中的数据
            $.ajax({
                url: top.$.rootUrl + '/ZZDT_EC/ec_enginedata_property/GetEnginePropById',
                data: { EngineDataID: EngineDataID, ProjectId: ProjectId, CaseID: CaseID },
                async: false,
                type: "GET",
                dataType: "json",
                success: function (data) {
                    if (data.code == 200 && data.data.length > 0)
                        selectData = data.data;
                },
                error: function () {
                },
                complete: function (data) {
                }
            });

            //查询
            $('#btn_Search').on('click', function () {
                page.search();
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 保存
            $('#lr_save').on('click', function () {
                var listEntity = [];
                //selectData = $.grep(selectData, function (item) {
                //    return item.PropertyValue != null && item.PropertyValue != '';
                //});
                for (var i = 0; i < selectData.length; i++) {
                    var param = {};
                    param.EngineDataPropertyID = selectData[i].EngineDataPropertyID;
                    param.EngineDataID = EngineDataID;
                    param.PropertyName = selectData[i].PropertyName;
                    param.PropertyValue = selectData[i].PropertyValue.trim();
                    param.CaseID = CaseID;
                    listEntity.push(param);
                }
                var postData = {
                    strEntity: JSON.stringify(listEntity)
                }
                $.lrSaveForm(top.$.rootUrl + '/ZZDT_EC/ec_enginedata_property/SaveEngineProp?ProjectId=' + ProjectId, postData, function (res) {

                });
            });
        },
        // 初始化列表
        initGird: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_property/GetPageList',
                headData: [
                    {
                        label: "操作", name: "op", width: 80, align: "center", sort: false,
                        formatter: function (value, row, op, $cell) {
                            return "<a style='color: #337ab7;text-decoration: underline;' href='javascript:showHistory(\"" + row.PropertyName + "\");'>历史数据</a>";
                        }
                    },
                    { label: "属性名称", name: "PropertyNameEN", width: 230, align: "left", sort: false },
                    { label: "属性类型", name: "PropertyTypeName", width: 80, align: "center", sort: false },
                    {
                        label: '属性值', name: 'PropertyValue', width: 220, align: "left", sort: false,
                        formatter: function (value, row, op, $cell) {
                            if (row.PropertyType == "00001" || row.PropertyType == "00005") {
                                //文字类型，输入长度不能超过1000。
                                return "<input type='text' id='my" + row.PropertyID + "' style='color: #333;border: none;width: 100%;outline:none;' maxlength='1000' onchange='ChangePropValue(this,\"" + row.PropertyName + "\",\"" + row.EngineDataPropertyID + "\")' value='" + value + "'/>";
                            } else if (row.PropertyType == "00002") {
                                //数字类型，只能输入数字，并需要控制小数点。
                                return "<input type='text' id='my" + row.PropertyID + "' style='color: #333;border: none;width: 100%;outline:none;' oninput='CheckDecimalPoint(this)' onblur='DecimalPoint(this,\"" + row.DecimalPoint + "\",\"" + row.PropertyName + "\",\"" + row.EngineDataPropertyID + "\")' value='" + value + "'/>";
                            } else if (row.PropertyType == "00003") {
                                //枚举类型需要根据枚举数据选择下拉框
                                var selectHtml = '<select id="my' + row.PropertyID + '" style="color: #333; border: none; width: 100%; outline: none;height: 100%;" onchange="ChangePropValue(this,\'' + row.PropertyName + '\',\'' + row.EngineDataPropertyID + '\')">';
                                selectHtml += '<option value="">==请选择==</option>';
                                if (row.EnumData) {
                                    $.ajax({
                                        url: top.$.rootUrl + '/ZZDT_EC/ec_dataitem/GetDetailTree',
                                        data: { itemCode: row.EnumData, ProjectId: ProjectId },
                                        async: false,
                                        type: "GET",
                                        dataType: "json",
                                        success: function (data) {
                                            if (data.code == 200 && data.data.length > 0)
                                                selectHtml += creatSelectTree(data.data, row.PropertyValue);
                                        },
                                        error: function () {
                                        },
                                        complete: function (data) {
                                        }
                                    });
                                }
                                selectHtml += '</select>';
                                return selectHtml;
                            }
                        },
                    },
                    {
                        label: "计量单位", name: "MeasuringUnitName", width: 120, align: "center", sort: false,
                        formatter: function (cellvalue,row) {
                            if (cellvalue) {
                                return cellvalue;
                            } else {
                                return row.DefaultUnitName;
                            }
                        }
                    },
                    { label: "创建人", name: "EngineCreateUserName", width: 100, align: "center", sort: false },
                    {
                        label: '创建时间', name: 'EngineCreateTime', width: 130, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm');
                        }},
                    { label: "更新人", name: "EngineUpdateUserName", width: 100, align: "center", sort: false },
                    {
                        label: '更新时间', name: 'EngineUpdateTime', width: 130, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm');
                        }},
                    { name: "", width: 1, sort: false }
                ],
                mainId: 'PropertyID',
                isPage: true,
                onRenderComplete: function (data) {
                    //默认已经选中的
                    if (selectData && selectData.length != 0) {
                        for (var i = 0; i < data.length; i++) {
                            var gdata = selectData.find(function (item) {
                                return item.PropertyName == data[i].PropertyName;
                            })
                            if (gdata) {
                                data[i].PropertyValue = gdata.PropertyValue;
                            }
                        }
                    }
                    $('#gridtable').jfGridSet("refreshdata", data);
                }
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            param.PropertyGID = PropertyGID;
            param.ObjectTypeID = ObjectTypeID;
            param.ProjectId = ProjectId;
            param.PropertyName = $("#PropertyName").val();
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param), engineDataID: EngineDataID, CaseID: CaseID });
        }
    };
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    //查看历史数据
    showHistory = function (propertyName) {
        learun.layerForm({
            id: 'propertyhis',
            title: '历史数据',
            url: top.$.rootUrl + '/ZZDT_EC/ec_enginedata_propertyhis/Index?propertyName=' + escape(propertyName) + '&EngineDataID=' + EngineDataID + "&ProjectId=" + ProjectId,
            width: 800,
            height: 600,
            btn: null
        });
    };
    page.init();
}

//正则检验  只能为数字
function CheckDecimalPoint(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, "");//清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g, "");//验证第一个字符是数字而不是字符
    obj.value = obj.value.replace(/\.{2,}/g, ".");//只保留第一个.清除多余的
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    //obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
}
//根据小数点决定显示几位小数
function DecimalPoint(obj, point, propertyName, engineDataPropertyID) {
    if (obj.value) {
        obj.value = Number(obj.value).toFixed(Number(point));
    }
    ChangePropValue(obj, propertyName, engineDataPropertyID);
}
//值改变之后
function ChangePropValue(obj, propertyName, engineDataPropertyID) {
    //去除空格和|_符号  但会改变option的值  所以注释
    //$(obj).find('option:selected')[0].text=$(obj).find('option:selected')[0].text.replace(/\s/g,'').replace('|_','')
    selectData = $.grep(selectData, function (item) {
        return item.PropertyName !== propertyName;
    });
    var param = {};
    param.PropertyValue = obj.value;
    param.PropertyName = propertyName;
    param.EngineDataPropertyID = engineDataPropertyID
    selectData.push(param);
}

var j = "";//前缀符号，用于显示父子关系，这里可以使用其它符号
function creatSelectTree(d, selectPropValue) {
    var option = "";
    for (var i = 0; i < d.length; i++) {
        if (d[i].ChildNodes && d[i].ChildNodes.length) {//如果有⼦集
            if (d[i].text == selectPropValue) {
                option += "<option value='" + d[i].text + "' selected>" + j + d[i].text + "</option>";
            } else {
                option += "<option value='" + d[i].text + "'>" + j + d[i].text + "</option>";
            }
            j += "&nbsp;&nbsp;&nbsp;|_";//前缀符号加⼀个符号
            if (j.length > 20) {
                j = j.replace('|_', '');
            }
            option += creatSelectTree(d[i].ChildNodes, selectPropValue);//递归调⽤⼦集
            //每次递归结束返回上级时，前缀符号需要减⼀个符号
            if (j.indexOf('|_') == -1) {
                j = j.slice(0, j.length - 18);
            } else {
                j = j.slice(0, j.length - 20);
            }
            if (j.length >= 18) {
                j += "|_";
            }
        } else {//没有⼦集直接显⽰
            if (d[i].text == selectPropValue) {
                option += "<option value='" + d[i].text + "' selected>" + j + d[i].text + "</option>";
            } else {
                option += "<option value='" + d[i].text + "'>" + j + d[i].text + "</option>";
            }
        }
    }
    return option;//返回最终html结果
}
